/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.notice.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.notice.dto.MsgConfigRelationBatchDTO;
import org.springblade.modules.notice.entity.BizMsgConfig;
import org.springblade.modules.notice.entity.BizMsgConfigRelation;
import org.springblade.modules.notice.enums.ChannelType;
import org.springblade.modules.notice.mapper.BizMsgConfigRelationMapper;
import org.springblade.modules.notice.service.IBizMsgConfigRelationService;
import org.springblade.modules.notice.service.IBizMsgConfigService;
import org.springblade.modules.notice.vo.BizMsgConfigRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务关联的消息模板 服务实现类
 *
 * @author ysq
 * @since 2024-11-20
 */
@Service
public class BizMsgConfigRelationServiceImpl extends BaseServiceImpl<BizMsgConfigRelationMapper, BizMsgConfigRelation> implements IBizMsgConfigRelationService {

	@Autowired
	private IBizMsgConfigService bizMsgConfigService;


	@Override
	public IPage<BizMsgConfigRelationVO> selectBizMsgConfigRelationPage(IPage<BizMsgConfigRelationVO> page, BizMsgConfigRelationVO bizMsgConfigRelation) {
		return page.setRecords(baseMapper.selectBizMsgConfigRelationPage(page, bizMsgConfigRelation));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean customSaveOrUpdate(MsgConfigRelationBatchDTO relationBatch) {
		BizMsgConfig config = bizMsgConfigService.getById(relationBatch.getBizMsgConfigId());
		if (config == null) {
			throw new ServiceException("业务消息不存在");
		}

		List<BizMsgConfigRelation> relationList = list(Wrappers.<BizMsgConfigRelation>lambdaQuery().eq(BizMsgConfigRelation::getBizMsgConfigId, relationBatch.getBizMsgConfigId()));
		if (relationBatch.getBizMsgConfigRelations() == null || relationBatch.getBizMsgConfigRelations().size() == 0) {
			if (relationList.isEmpty()) {
				return true;
			}
			return remove(Wrappers.<BizMsgConfigRelation>lambdaQuery().eq(BizMsgConfigRelation::getBizMsgConfigId, relationBatch.getBizMsgConfigId()));
		}

		// 去重
		List<String> distinct = relationBatch.getBizMsgConfigRelations().stream().map(t -> {
			String str = String.format("%s-%s-%s-%s",t.getBusinessType(), t.getChannelType(), t.getPushType(), t.getMsgTemplateId());
			return str;
		}).distinct().collect(Collectors.toList());
		if (distinct.size() != relationBatch.getBizMsgConfigRelations().size()) {
			throw new ServiceException("关联的消息模板不能重复");
		}

		relationBatch.getBizMsgConfigRelations().forEach(relation -> {
			if (!ChannelType.FEI_SHU_ROBOT.getCode().equals(relation.getChannelType()) && Func.isBlank(relation.getPushType())) {
				throw new ServiceException(String.format("%s模板推送对象不能为空", ChannelType.getDesc(relation.getChannelType())));
			}
			relation.setBizMsgConfigId(relationBatch.getBizMsgConfigId());
		});

		List<Long> removeIds = relationList.stream().map(BizMsgConfigRelation::getId).collect(Collectors.toList());
		return this.saveBatch(relationBatch.getBizMsgConfigRelations()) && removeIds.size() > 0 ? removeByIds(removeIds) : true;
	}


	@Override
	public List<BizMsgConfigRelationVO> getDetailsByConfigId(Long bizMsgConfigId) {
		return baseMapper.getDetailsByConfigId(bizMsgConfigId);
	}
}
